/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Experiment;

import entities.Company;
import entities.Survey;
import entities.User;
import entities.UserAnswers;
import helpers.Surveys;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import db.DB;
import entities.Target;
import helpers.RandomGenerator;
import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 *
 * @author arnaldo
 */
public class Experiment {

    private int numUsers;
    private int numCompanies;
    private int surveysPerUser;
    private int surveysPerCompany;
    private Company[] companies;
    private User[] users;
    private ArrayList<Surveys> surveys;
    private DB db;
    private FileWriter fstream;
    private BufferedWriter out;

    public Experiment(int numUsers, int numCompanies, int surveysPerUser, int surveysPerCompany) {

        this.numUsers = numUsers;
        this.numCompanies = numCompanies;
        this.surveysPerUser = surveysPerUser;
        this.surveysPerCompany = surveysPerCompany;

        this.companies = new Company[numCompanies];
        this.users = new User[numUsers];

        this.surveys = new ArrayList<Surveys>();
        this.db = new DB(DB.db.MySQL);

        RandomGenerator.initTargets();

        try {
            // Create file
            this.fstream = new FileWriter("data.sql");
            this.out = new BufferedWriter(fstream);

            this.generateRaces();
            this.generateTargets();
            this.generateCompanies();
            this.generateUsers();

            //out.write(data);

            //Close the output stream
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    private void generateRaces() throws IOException {
        String[] races = RandomGenerator.races;
        out.write("# Races Insert\n");
        for (int i = 0; i < races.length; i++) {
            String ri = this.db.RaceInsert(races[i]);
            out.write(ri + "\n");
        }
    }

    private void generateTargets() throws IOException {
        Target[] targets = RandomGenerator.targets;

        out.write("\n# Targets Insert\n");
        for (int i = 0; i < targets.length; i++) {
            String ti = this.db.TargetInsert(targets[i]);
            out.write(ti + "\n");
        }
    }

    private void generateCompanies() throws IOException {
        out.write("# Companies Insert \n");
        for (int i = 0; i < this.numCompanies; i++) {

            this.companies[i] = new Company();
            this.companies[i].setId(this.db.lastCompanyId);
            String ci = this.db.CompanyInsert(companies[i]);
            out.write(ci + "\n");
            out.write("# Surveys created by this company \n");

            for (int j = 0; j < this.surveysPerCompany; j++) {

                Survey s = this.companies[i].createSurvey();
                s.setId(this.db.lastSurveyId);
                surveys.add(new Surveys(s, this.companies[i]));
                String si = this.db.SurveyInsert(s, s.getTarget());
                String csi = this.db.CompanySurveyInsert(this.companies[i], s);

                out.write(si + "\n");
                out.write(csi + "\n");

                out.write("# Questions for this Survey\n");
                for (int k = 0; k < s.getQuestions().length; k++) {
                    s.getQuestions()[k].setId(this.db.lastQuestionId);
                    String qi = this.db.QuestionInsert(s.getQuestions()[k]);
                    out.write(qi + "\n");
                }

            }

            out.write("\n#  ----------------------------- \n\n");
        }
    }

    private void generateUsers() throws IOException {

        Random r = new Random();
        int n = this.surveys.size();

        out.write("\n# Users insert \n");

        for (int i = 0; i < this.numUsers; i++) {
            this.users[i] = new User();
            this.users[i].setId(this.db.lastUserId);
            String ui = this.db.UserInsert(this.users[i]);
            out.write(ui + "\n");
            out.write("\n# Survey taken by this user\n");

            for (int j = 0; j < this.surveysPerUser; j++) {
                Surveys s = this.surveys.get(r.nextInt(n));
                UserAnswers[] ua = this.users[i].takeSurvey(s.getSurvey());
                String usi = this.db.UserSurveyInsert(this.users[i], s.getSurvey());
                out.write("\n\n" + usi + "\n");
                out.write("# Answers for this survey\n");

                for (int k = 0; k < ua.length; k++) {
                    String uai = this.db.UserAnswerInsert(this.users[i], ua[k].getQuestion(), s.getSurvey(), ua[k].getAnswer());
                    out.write(uai + "\n");
                }

                //handle all this data into the db
            }
        }
    }
}
